source("./code/loadPackages.R")

# Load in the data
ayu = fread("./data/principalsHawkData.csv")


#### Figure 3: Predicted Hawkishess Measures for Senior US Decision-Makers ####

# Create limits on the plot
hmin=0; hmax=1

# President
presd = ayu[which(ayu$pos_name=="President of the United States"),]
presd = presd %>% filter(!(firstlast %in% c("George Bush")))
presd$firstlast = factor(presd$firstlast, 
                         levels=c("Harry Truman", "Dwight Eisenhower", "John Kennedy",
                                  "Lyndon Johnson", "Richard Nixon", "Gerald Ford", 
                                  "Jimmy Carter", "Ronald Reagan"))

prhawk = ggplot(presd, aes(hawkMean, fct_rev(firstlast))) + 
  geom_pointrange(size=0.4, aes(xmin=hawkLower, xmax=hawkUpper)) + 
  theme_bw() + scale_x_continuous("Hawkishness", limits=c(hmin, hmax)) + ylab("President") +
  theme(legend.position="none")


# Secretary of State
secstd = ayu[which(ayu$pos_name=="Secretary of State"),]
unsecstd = secstd[!duplicated(secstd$firstlast),]
secstd$firstlast = factor(secstd$firstlast, levels=unsecstd$firstlast[order(unsecstd$pos_start, decreasing = T)])

sshawk = ggplot(secstd, aes(hawkMean, factor(firstlast))) + 
  geom_pointrange(size=0.4, aes(xmin=hawkLower, xmax=hawkUpper)) + 
  theme_bw() + scale_x_continuous("Hawkishness", limits=c(hmin, hmax)) + ylab("Secretary of State") +
  theme(legend.position="none")


# Secretary of Defense
secdefd = ayu[which(grepl("^Secretary of Defense|8th United States Secretary of Defense", ayu$pos_name)),]
unsecdefd = secdefd[!duplicated(secdefd$firstlast),]
secdefd$firstlast = factor(secdefd$firstlast, levels=unsecdefd$firstlast[order(unsecdefd$pos_start, decreasing = T)])

sdhawk = ggplot(secdefd, aes(hawkMean, (firstlast))) + 
  geom_pointrange(size=0.4, aes(xmin=hawkLower, xmax=hawkUpper)) + 
  theme_bw() + scale_x_continuous("Hawkishness", limits=c(hmin, hmax)) + ylab("Secretary of Defense") +
  theme(legend.position="none")


# Director of Central Intelligence
dcid = ayu[which(grepl("^Director of Central Intelligence|^Director of the Central Intelligence|^Director of the CIA", ayu$pos_name)),]
undcid = dcid[!duplicated(dcid$firstlast),]
dcid$firstlast = factor(dcid$firstlast, levels=undcid$firstlast[order(undcid$pos_start, decreasing = T)])

dcihawk = ggplot(dcid, aes(hawkMean, (firstlast))) + 
  geom_pointrange(size=0.4, aes(xmin=hawkLower, xmax=hawkUpper)) + 
  theme_bw() + scale_x_continuous("Hawkishness", limits=c(hmin, hmax)) + ylab("Director of Central Intelligence") +
  theme(legend.position="none")


# Chairman of Joint Chiefs of Staff
cjcsd = ayu[grepl("^Chairman of the Joint Chiefs of Staff|^Chairman of the Joint Chief", ayu$pos_name),]
uncjcsd = cjcsd[!duplicated(cjcsd$firstlast),]
cjcsd$firstlast = factor(cjcsd$firstlast, levels=uncjcsd$firstlast[order(uncjcsd$pos_start, decreasing = T)])

cjcshawk = ggplot(cjcsd, aes(hawkMean, (firstlast))) + 
  geom_pointrange(size=0.4, aes(xmin=hawkLower, xmax=hawkUpper)) + 
  theme_bw() + scale_x_continuous("Hawkishness", limits=c(hmin, hmax)) + ylab("Chairman of the Joint Chiefs of Staff") +
  theme(legend.position="none")


# National Security Adviser
nsad = ayu[which(ayu$pos_name=="National Security Advisor"),]
nsad$firstlast[which(nsad$actorYear=="Robert Cutler 1957")] = "Robert Cutler (2)"
unnsad = nsad[!duplicated(nsad$firstlast),]
nsad$firstlast = factor(nsad$firstlast, levels=unnsad$firstlast[order(unnsad$pos_start, decreasing = T)])

nsahawk = ggplot(nsad, aes(hawkMean, (firstlast))) + 
  geom_pointrange(size=0.4, aes(xmin=hawkLower, xmax=hawkUpper)) + 
  theme_bw() + scale_x_continuous("Hawkishness", limits=c(hmin, hmax)) + ylab("National Security Advisor") +
  theme(legend.position="none")


# Plot all together
hawkplots = ggpubr::ggarrange(prhawk, sshawk, sdhawk, dcihawk, cjcshawk, nsahawk, 
                              ncol=2, nrow=3, common.legend=TRUE, legend="bottom", align="v")
print(hawkplots)

ggsave("./figures/hawkPlot.pdf", plot=hawkplots, height=9, width=7, units='in')





#### Figure 4: Average Speaker Hawkishness in US Foreign Policy Meetings ####

## Load meeting data
nscd_all = fread("./data/meetingData.csv")
nscd_all$admin = factor(nscd_all$admin, levels=c("Truman", "Eisenhower", "Kennedy", "Johnson", "Nixon", "Ford", "Carter", "Reagan"))
nscd_all$date = as.Date(nscd_all$date)


cbPalette <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")

meetPlot = ggplot(nscd_all, aes(date, meanHawk)) + 
  geom_point(aes(color=admin, shape=factor(formal)), alpha=0.5) + theme_bw() +
  ylim(0.35, 0.70) +
  scale_color_manual("Admin.", values=cbPalette) + 
  scale_shape_discrete("Meeting", labels=c("Informal", "Formal")) +
  xlab("Year") + ylab("Meeting hawkishness")
meetPlot

ggsave(filename="./figures/meetPlot.pdf", 
       height=4, width=7, units='in')



#### Table 2: Decisions Against Adversaries by Administration ####
tableDecs = nscd_all %>% group_by(admin) %>% summarize(decs=sum(nConfAdv + nCoopAdv),
                                                       nConfAdv=sum(nConfAdv),
                                                       nCoopAdv=sum(nCoopAdv))
tableDecs
apply(tableDecs[,2:4], 2, sum)



#### Table A1: Coverage of formal NSC records for each administration ####
tableFormal = nscd_all |> filter(formal==1) |> group_by(admin) %>% summarize(recs=length(unique(meetNum)), 
                                                          speechacts=sum(nSegs),
                                                          decs=sum(nConfAdv + nCoopAdv),
                                                          nConfAdv=sum(nConfAdv),
                                                          nCoopAdv=sum(nCoopAdv))
tableFormal
apply(tableFormal[,2:6], 2, sum)

#### Table A2: Coverage of informal meeting records for each administration ####
tableInformal = nscd_all |> filter(formal==1) |> group_by(admin) %>% summarize(recs=length(unique(meetNum)), 
                                                           speechacts=sum(nSegs),
                                                           decs=sum(nConfAdv + nCoopAdv),
                                                           nConfAdv=sum(nConfAdv),
                                                           nCoopAdv=sum(nCoopAdv))
tableInformal
apply(tableInformal[,2:6], 2, sum)


#### Figure A3: Distributions of Foreign Policy Decisions ####
ggplot(nscd_all, aes(nConfAdv)) + geom_histogram(aes(fill=factor(formal)), binwidth=1) +
  theme_bw() + scale_fill_manual("Meeting", values=c("gray70", "gray30"), labels=c("Informal", "Formal")) +
  scale_x_continuous("Conflictual Decisions toward Adversary", breaks=c(0,2,4,6,8)) + ylab("Count")

ggsave(filename="./figures/histOutcomeCount.pdf", height=3, width=4, units='in')

ggplot(nscd_all, aes(nConfAdv-nCoopAdv)) + geom_histogram(aes(fill=factor(formal)), binwidth=1) +
  theme_bw() + scale_fill_manual("Meeting", values=c("gray70", "gray30"), labels=c("Informal", "Formal")) +
  scale_x_continuous("Conflictual Decisions - Cooperative Decisions", breaks=c(-6, -3, 0, 3, 6)) + ylab("Count")

ggsave(filename="./figures/histOutcomeDiff.pdf", height=3, width=4, units='in')